# Dickstein, Ho, and Mark (2023)
# This file estimates the demand system.

# The way the code is structured is as follows: 
# - In the estimationcode folder, there exists a number of files that are called to estimate a demand specification. 
# - In the specs folder, there are a number of subfolder that contain the different specifications. In each of these 
# subfolders, there is a call_estimation.R file. In this file, the specification is defined. If you run this file, 
# the specification will be estimated, with results being saved in a "output" folder within the specification subfolder.
# - In two of the specs folders (where the cost cut off is $20, there are "bootstrap" folders, where the bootstrap 
# demand systems are estimated.

# If you want to generate your own specification, make a new specification folder and a new call_estimation.R file. 
# In that file, you need to define the following options: 
# 1. specs_location (location of the specs folder) 
# 2. specs_name (what is the name of the specification? This should match the folder name * in specs/*)
# 3. testing (T/F for whether you want this run to be a test run)
# 4. output_path (where do you want to put the outputs?)
# 5. data (the household-option level dataset)
# 6. var_names - List of the variables you want to use in the specification: 
# var_names = list(W1_names = , W2_names = , W3_names = , Z_names = )
# 7. var_switch - List of the of the variables you want to interact switcher with: 
# var_switch = list(W1_names = , ...) These should be dummy variables for "interact"
# 8. est_type (character vector which is one the the three: "full_only", "switcher_only", "mixed"
# 9. fixed_cost_censor (cost censor in $100 per month (e.g. 0.2))

# You can run the whole system by running the code below. 

# Setting preliminaries
module remove R
module load R/4.0.2
WORK=$(pwd)

#Estimating
# Preferred specifications:
cd $WORK/specs/lowrisk_simplemh_censor0.2_switcher
Rscript call_estimatedemand.R 
cd $WORK/specs/lowrisk_simplemh_censor0.2_main
Rscript call_estimatedemand.R
# $50 cost cutoff specifications:
cd $WORK/specs/lowrisk_simplemh_censor0.5_switcher
Rscript call_estimatedemand.R 
cd $WORK/specs/lowrisk_simplemh_censor0.5_main
Rscript call_estimatedemand.R
# Fit analysis specification: 
cd $WORK/specs/lowrisk_simplemh_censor0.2_80sample_main
Rscript call_estimatedemand.R
# Bootstrap Specifications:
#cd $WORK/specs/lowrisk_simplemh_censor0.2_switcher/bootstrap
#Rscript run_bootstrap.R 
#cd $WORK/specs/lowrisk_simplemh_censor0.2_main/bootstrap
#Rscript run_bootstrap.R 

# If you want to clean up the folder so that no output remains, uncomment and run the following code.
#cd $WORK/specs/lowrisk_simplemh_censor0.2_switcher
#rm -r output
#cd $WORK/specs/lowrisk_simplemh_censor0.2_main
#rm -r output
#cd $WORK/specs/lowrisk_simplemh_censor0.5_switcher
#rm -r output
#cd $WORK/specs/lowrisk_simplemh_censor0.5_main
#rm -r output
#cd $WORK/specs/lowrisk_simplemh_censor0.2_80sample_main
#rm -r output
#cd $WORK/specs/lowrisk_simplemh_censor0.2_switcher/bootstrap
#rm -r output*
#cd $WORK/specs/lowrisk_simplemh_censor0.2_main/bootstrap
#rm -r output*
